package com.smooware.icms.adapter.list;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.smooware.icms.R;
import com.smooware.icms.data.model.Inventory;

public class WOInventoryListAdapter extends ArrayAdapter<Inventory> {

	private static final int NOT_PLURAL = 1;

	public WOInventoryListAdapter(final Context context) {
		super(context, R.layout.wo_inventory_list_row,
				R.id.wo_inventory_item_description, new ArrayList<Inventory>());
	}

	public WOInventoryListAdapter(final Context context,
			final List<Inventory> inventories) {
		super(context, R.layout.wo_inventory_list_row,
				R.id.wo_inventory_item_description, inventories);
	}

	@Override
	public View getView(final int position, final View convertView,
			final ViewGroup parent) {
		View view = super.getView(position, convertView, parent);

		Inventory inventory = getItem(position);

		initItemNumber(view, inventory);
		initSKU(view, inventory);
		initUPC(view, inventory);
		initCount(view, inventory);

		return view;
	}

	private void initItemNumber(final View view, final Inventory inventory) {
		TextView itemNumberView = (TextView) view
				.findViewById(R.id.wo_inventory_item_number);
		itemNumberView.setText("" + inventory.getIcmsItemNumber());
	}

	private void initSKU(final View view, final Inventory inventory) {
		TextView skuView = (TextView) view
				.findViewById(R.id.wo_inventory_item_SKU);
		skuView.setText(inventory.getItemSKU());
	}

	private void initUPC(final View view, final Inventory inventory) {
		TextView upcView = (TextView) view
				.findViewById(R.id.wo_inventory_item_UPC);
		upcView.setText(inventory.getItemUPC());
	}

	private void initCount(final View view, final Inventory inventory) {
		TextView countView = (TextView) view
				.findViewById(R.id.wo_inventory_item_count);
		if (inventory.getCount() > NOT_PLURAL) {
			countView.setText("" + inventory.getCount()
					+ getContext().getString(R.string.pcs));
		} else {
			countView.setText("" + inventory.getCount()
					+ getContext().getString(R.string.pc));
		}
	}
}
